package com.taojin.shoppinginterest.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.adapter.MyAdapter;
import com.taojin.shoppinginterest.bean.Distance;
import com.taojin.shoppinginterest.bean.DistrictList;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.qianfeng.zw.day43.pop
 * @作 用:
 * @创 建 人: zhangwei
 * @日 期: 15/5/6 15:05
 * @修 改 人:
 * @日 期:
 */
public class PopHelper {

    private static PopHelper helper;
    private PopupWindow popupWindow;
    private List<String> dataLeft;
    private List<String> dataRight;

    /**
     * 先考虑焦点
     *
     * @return
     */
    public static PopHelper getInstance() {
        if (helper == null) {
            helper = new PopHelper();
        }
        return helper;
    }



    private void init(Context context) {
        popupWindow = new PopupWindow(context);
        popupWindow.setBackgroundDrawable(new ColorDrawable(R.color.black));
    }

    /**
     * @param width
     * @param height
     */
    private void init(Context context, int width, int height) {
        this.init(context);
        popupWindow.setHeight(height);
        popupWindow.setWidth(width);
    }

    /**
     * @param width
     * @param height
     */

    public void init(Context context, View contentView, View anchor, int width, int height) {
        popupWindow = new PopupWindow(context);
        //设置popupwindow高度
        popupWindow.setHeight(500);
        //设置popupwindow宽度
        popupWindow.setWidth(width);
        popupWindow.setFocusable(true);
        //设置popupwindow的显示界面
        popupWindow.setBackgroundDrawable(new ColorDrawable(R.color.wheat) {
        });
        popupWindow.setOutsideTouchable(true);
        popupWindow.setContentView(contentView);

        //设置popupwindow显示的父控件


        popupWindow.showAsDropDown(anchor);
    }


    public void init(Context context, View anchor,List<DistrictList> list) {
        this.init(context, createContentView2(context,list), anchor, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    }


    /**
     * 创建Popupwindow内容显示界面
     * @param context
     * @return
     */
    private View createContentView(final Context context) {
        LayoutInflater inflate = LayoutInflater.from(context);
        View contentView = inflate.inflate(R.layout.fragment_favorable_pop, null);
        ListView listView = (ListView) contentView.findViewById(R.id.fragment_favorable_pop_left);
        final ArrayList<String> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            data.add("pop-------->" + i);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, data.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, android.R.id.text1, data);
        listView.setAdapter(arrayAdapter);



//        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        return contentView;
    }

    private View createContentView2(final Context context ,List<DistrictList> list) {
        LayoutInflater inflate = LayoutInflater.from(context);
        View contentView = inflate.inflate(R.layout.fragment_favorable_pop, null);
        final ListView listLeft= (ListView) contentView.findViewById(R.id.fragment_favorable_pop_left);
        ListView listRight= (ListView) contentView.findViewById(R.id.fragment_favorable_pop_right);
        listLeft.setAdapter(new MyAdapter(getDataLeft(list),context));
        listRight.setAdapter(new MyAdapter(getDataRight(list),context));
        listLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //view.setFocusable(true);
                view.setSelected(true);
                TextView textView = (TextView) view.findViewById(R.id.pop_tv);
                textView.setSelectAllOnFocus(true);


            }
        });
        listRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });


       return contentView;
    }
    public void dismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    public boolean isShowing() {
        return popupWindow != null && popupWindow.isShowing() ? true : false;
    }
    private List<String> getDataLeft(List<DistrictList> list){
        if(list!=null&&!list.isEmpty()) {
            dataLeft = new ArrayList<>();
            dataLeft.add("附近");
            dataLeft.add("新街口");
            dataLeft.add("夫子庙");
            dataLeft.add("河西");
            dataLeft.add("湖南路");
            dataLeft.add("更多商圈陆续登陆");


        }
        return dataLeft;

    }
    private List<String> getDataRight(List<DistrictList> list){
        if(list!=null&&!list.isEmpty()) {
            dataRight = new ArrayList<>();
            List<Distance> distanceList=list.get(0).getList();
            if(distanceList!=null&&!distanceList.isEmpty()){
            for(int i=0;i<distanceList.size();i++){
                dataRight.add(distanceList.get(i).getAlias());
            }

            }
        }
        return dataRight;

    }


}
